(function($,undefined){
	//初始化默认设置
	var defaults={
			server:"",
			icon:null,
			onSelect:function(){}, //菜单节点触摸事件
			nodeId:"id",
			nodeParentId:"parent.id",
			url:"#",
			menuTempl:null,
			openLevel:0,
			dataSource:null,
			parentNode:[]
			
			
	};
    
	$.fn.mobileMenu=function(params,bindings){
		var options=$.extend({},defaults,params);
		var containerId=$(this).attr("id");
		if(!containerId)
			return;
		var g={
		     _init:function(options){
			        options.dataSource=searchMenu(options);
					var pagefn = doT.template($(options.menuTempl).text(), undefined, undefined);
					$("#"+containerId).html(pagefn(options.dataSource));
					
					//绑定事件函数
					g._bindEvent(options.dataSource,bindings);
					if(options.openLevel>0){
						g._backMenuHistory(options);
					}
					 if($("#"+containerId).closest("[data-role='scrollview']"))
						 $("#"+containerId).closest("[data-role='scrollview']").scrollview("refresh",0,true);
		      },
		      _bindEvent:function(p,bindings){
		    	  var menuElements=$("a",$("#"+containerId));
		    	  menuElements.each(function(index,dom){
		    		     $(dom).on("tap",function(e){
//		    		    	 if(!p[index]['url']||p[index]['url']==null){
			    		    	 if(bindings&&bindings.onSelect&&typeof bindings.onSelect==="function"&&p[index]['hasChild']=='false'){
				    				  e.preventDefault();
				    				  bindings.onSelect.call(options.onSelect,p[index]);
				    			  }
				    			  else if(p[index]['hasChild']=='true'){
				    				  e.preventDefault();
				    				  options.parentNode.push({"openLevel":options.openLevel,"id":p[index][options.nodeParentId]});
				    				  g._openNextNodes(options,p[index]);
				    			  }
//			    		    }
		    		      });
		    		  
		    	  });
		    	  
		    	  
		      },
		      _openNextNodes:function(options,selectedNode){
		    	  options[options.nodeId]=selectedNode['id'];
		    	  options.openLevel+=1;
		    	  g._init(options);
		      },
		      _backMenuHistory:function(p){
		    	  var backBtnHtml='<li  >'
		    		  +'<a href="javascript:void(0);" class="back-menu" style="display:none;"><span>返回</span><i class="icon-arrow-left"></i></a>' 
		    		  +'</li>';
		    	  $("#"+containerId).prepend(backBtnHtml);
		    	  if(options.openLevel>0){
		    			    $("a.back-menu",$("#"+containerId)).show();
		    	  };  
	    		  $("a.back-menu",$("#"+containerId)).unbind("tap");
	    		  $("a.back-menu",$("#"+containerId)).on("tap",function(){
	    			  options.openLevel-=1;
	    			$.each(options.parentNode,function(key,value){
	    				       if(value['openLevel']==options.openLevel)
	    				    	   options[options.nodeId]=value["id"];
	    			  });
	    			  g._init(options);
	    		  });
	    		  // 新增滑动回退事件
	    		  console.log(options.openLevel)
	    		  $("#"+containerId).closest(".ui-page").unbind("swiperight").bind("swiperight",function(){
	    			    options.openLevel-=1;
		    			$.each(options.parentNode,function(key,value){
		    				       if(value['openLevel']==options.openLevel)
		    				    	   options[options.nodeId]=value["id"];
		    			  });
		    			  g._init(options);
	    			     console.log("swipeleft back");
	    		  });
		      }
				
				
		};
		//初始化
		g._init(options);
		
		
	};
	//查询上上级菜单
	var getParentsMenu=function(params){
		var parentsData={};
		if(params.server){
			Newtouch.Ajax.navigationSendParams(params,params.server,function(data){
				if(data!=undefined||data.length!=0){
					parentsData=data;
				}
			 }, function(error){
			
			 }, true);
		}
		return parentsData;
		
	};
	
	//查询菜单数据
	var searchMenu=function(params){
		var menuData={};
		if(params.server){
			Newtouch.Ajax.navigationSendParams(params,params.server,function(data){
				if(data!=undefined||data.length!=0){
					menuData=data;
				}
			 }, function(error){
			
			 }, true);
		}
		return menuData;
	};
	
	
	
	
})(jQuery);